<template>
  <CommonCard title="累计用户数" value="117,796,5">
    <template #default>
      <div ref="chartRef" style="width: 100%; height: 100%"></div>
    </template>
    <template #footer>
      <div class="footer">
        <span>日同比</span>
        <span class="common-data">17.70%</span>
        <span class="increment"></span>
        <span>月同比</span>
        <span class="common-data">47.12%</span>
        <span class="decrement"></span>
      </div>
    </template>
  </CommonCard>
</template>

<script lang="ts">
import CommonCard from './CommonCard.vue'
export default {
  name: 'TotalUsers',
  components: {
    CommonCard,
  },
}
</script>

<script lang="ts" setup>
import * as echarts from 'echarts'
import { ref, onMounted } from 'vue'

const chartRef = ref()
let myChart
const getOptions = () => {
  return {
    grid: {
      left: 0,
      right: 0,
    },
    xAxis: {
      show: false,
      min: 0,
      max: 100,
    },
    yAxis: {
      show: false,
      data: ['数据'],
    },
    series: {
      type: 'bar',
      data: [44],
      // 设置柱条的宽度
      barWidth: 10,
      // 设置标签字体样式
      label: {
        show: true,
        formatter: '|',
        position: 'right'
      },
      // 显示柱条的背景颜色
      showBackground: true,
      // 柱条的颜色
      itemStyle: {
        color: 'orangered'
      }
    },
  }
}

onMounted(() => {
  myChart = echarts.init(chartRef.value)
  myChart.setOption(getOptions())

  // 当浏览器宽度发生变化的时候，图表也要发生变化
  window.addEventListener('resize', () => {
    myChart && myChart.resize()
  })
})
</script>

<style lang="scss" scoped>
.footer {
  display: flex;
  align-items: center;
  .increment {
    margin-right: 10px;
  }
}
</style>
